/*
 * author      : 吴中勤
 * date        : 2018/8/22
 * description : 车辆转换 sap -> moby
 */

package com.deepexi.adapter.rest.sap.parser;

import com.deepexi.adapter.common.StringUtil;
import com.deepexi.adapter.facade.sap.request.SapPushExteriorPartBody;
import com.deepexi.adapter.sdk.moby.request.ExteriorPart;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

public class SapPushExteriorPartParser extends BaseParser<ExteriorPart, SapPushExteriorPartBody> {

    @Override
    protected void convertEachSapItem(SapPushExteriorPartBody sapItem) {

        if (StringUtil.isNullorEmpty(sapItem.getModelCode())) {
            logger.warn("车型外观,缺少 车型, {}", sapItem.uglyString());
            return;
        }
        if (StringUtil.isNullorEmpty(sapItem.getModelLineCode())) {
            logger.warn("车型外观,缺少 车系, {}", sapItem.uglyString());
            return;
        }
        if (StringUtil.isNullorEmpty(sapItem.getCode())) {
            logger.warn("车型外观,缺少 外观编码, {}", sapItem.uglyString());
            return;
        }
        if (StringUtil.isNullorEmpty(sapItem.getText())) {
            logger.warn("车型外观,缺少 外观文本, {}", sapItem.uglyString());
            return;
        }

        ExteriorPart mobyItem = new ExteriorPart();
        mobyItem.setModelCode(sapItem.getModelCode());
        mobyItem.setBrandCode("");
        mobyItem.setModelLineCode(sapItem.getModelLineCode());
        mobyItem.setCode(sapItem.getCode());
        mobyItem.setName(sapItem.getText());
        mobyRequestBodyList.add(mobyItem);
    }

    @Override
    protected List<SapPushExteriorPartBody> parseSapListFromRecordsStr(String recordsStr) {
        Type collectionType = new TypeToken<List<SapPushExteriorPartBody>>() {
        }.getType();
        return new Gson().fromJson(recordsStr, collectionType);
    }
}
